package show.me.the.money.service.core.event.test.tr;

import java.util.Date;
import java.util.HashMap;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import show.me.the.money.dao.SimpleDAO;
import show.me.the.money.dao.exception.DataAccessException;
import show.me.the.money.service.dao.DAOSupportService;
import show.me.the.money.util.context.Context;


public class MyDao extends DAOSupportService {

	@Override
	public void invoke(HashMap<String, Object> req,
			HashMap<String, Object> res, Context ctx, SimpleDAO dao)
			throws DataAccessException {
		HashMap<String, Object> rec = new HashMap<String, Object>();
		rec.put("name", "wuk2");
		dao.create(rec);
		dao.rollbackTransaction();
	}
	
	public static void main(String[] args) {
		FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/config/spring/spring-hibernate.xml");
		SessionFactory sf = (SessionFactory) ctx.getBean("mySessionFactory");
		Session ss = sf.openSession();
		Transaction ta = ss.getTransaction();
		ta.begin();
		ss.save("t_person", createRecord());
//		ta.rollback();
		ta.commit();
		ss.clear();
//		ss.clear();
		ta.begin();
		ss.save("t_person", createRecord());
		ta.rollback();
		ss.clear();
//		ta.commit();
		ta.begin();
		ss.save("t_person", createRecord());
		ta.commit();
		ss.clear();
	}
	static int aa = 100000;
	private static HashMap<String, Object> createRecord(){
		HashMap<String, Object> rec = new HashMap<String, Object>();
		rec.put("id", String.valueOf(++aa));
		rec.put("name", "super_wuk");
		return rec;
	}

}
